package com.aoip.telemetry.telemetry.configureui.business.order.cases.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aoip.telemetry.telemetry.configureui.business.order.cases.service.OrderCaseService;
import com.aoip.telemetry.telemetry.configureui.business.order.config.service.OrderConfigService;
import com.aoip.telemetry.telemetry.configureui.common.annotation.Log;
import com.aoip.telemetry.telemetry.configureui.common.core.controller.BaseController;
import com.aoip.telemetry.telemetry.configureui.common.core.page.TableDataInfo;
import com.aoip.telemetry.telemetry.configureui.common.enums.BusinessType;
import com.aoip.telemetry.telemetry.configureui.common.utils.StringUtils;
import com.aoip.telemetry.telemetry.core.entity.AjaxResult;
import com.aoip.telemetry.telemetry.entity.enums.AbnoTypeEnum;
import com.aoip.telemetry.telemetry.entity.order.OrderBaseEntity;
import com.aoip.telemetry.telemetry.entity.order.RelOrderStEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 指令实例创建
 *
 * @author chenbai
 * @version 1.0
 * @date 2022/2/22 16:49
 */
@RestController
public class OrderCaseController extends BaseController {

    @Resource
    private OrderCaseService service;

    @Resource
    private OrderConfigService orderConfigService;

    @Resource
    private OrderCaseService orderCaseService;

    /**
     * 获取指令实例分页列表
     *
     * @param entity 测站
     * @return com.aoip.telemetry.telemetry.configureui.common.core.list.TableDataInfo
     * @author chenbai
     * @date 2022/2/22
     **/
    @GetMapping("/v1/GET/order/case/page")
    public TableDataInfo page(OrderBaseEntity entity) {
        startPage();
        List<OrderBaseEntity> list = service.list(entity);
        return getDataTable(list);
    }

    /**
     * 指令实例不分页列表
     *
     * @param entity 测站
     * @return com.aoip.telemetry.telemetry.core.entity.AjaxResult
     * @author chenbai
     * @date 2022/2/22
     **/
    @GetMapping("/v1/GET/order/case/list")
    public AjaxResult list(OrderBaseEntity entity) {
        List<OrderBaseEntity> list = service.list(entity);
        return AjaxResult.success(list);
    }

    /**
     * 根据测站编码查询配置的指令列表
     *
     * @param stCode 测站编码
     * @return com.aoip.telemetry.telemetry.core.entity.AjaxResult
     * @author chenbai
     * @date 2022/2/24
     **/
    @GetMapping("/v1/GET/order/case/list/{stCode}")
    public AjaxResult orderList(@PathVariable String stCode) {
        RelOrderStEntity entity = new RelOrderStEntity();
        entity.setStCode(stCode);
        List<RelOrderStEntity> list = orderConfigService.list(entity);
        List<OrderBaseEntity> orderList = new ArrayList<>();
        list.forEach(item -> {
            String orderId = item.getOrderId();
            if (StringUtils.isNotEmpty(orderId)) {
                OrderBaseEntity order = orderCaseService.order(orderId);
                if (order != null) {
                    orderList.add(order);
                }
            }
        });
        if (orderList.size() < 1) {
            return AjaxResult.error("测站未配置相关召测指令！");
        }
        return AjaxResult.success(orderList);
    }

    /**
     * 获取单个指令实例
     *
     * @param id 指令实例主键
     * @return com.aoip.telemetry.telemetry.core.entity.AjaxResult
     * @author chenbai
     * @date 2022/2/22
     **/
    @GetMapping("/v1/GET/order/case/{id}")
    public AjaxResult station(@PathVariable String id) {
        return AjaxResult.success(service.order(id));
    }



    @Log(title = "指令实例", businessType = BusinessType.INSERT)
    @PostMapping("/v1/POST/order/case")
    public AjaxResult add(@Validated @RequestBody JSONObject params) {
        OrderBaseEntity entity = JSON.parseObject(JSON.toJSONString(params.get("entity")), OrderBaseEntity.class);
        List<RelOrderStEntity> relOrderSt = JSON.parseArray(JSON.toJSONString(params.get("relOrderSt")), RelOrderStEntity.class);
        return toAjax(service.insert(entity, relOrderSt));
    }

    /**
     * 修改指令实例
     */
    @Log(title = "指令实例", businessType = BusinessType.UPDATE)
    @PutMapping("/v1/PUT/order/case")
    public AjaxResult edit(@Validated @RequestBody JSONObject params) {
        OrderBaseEntity entity = JSON.parseObject(JSON.toJSONString(params.get("entity")), OrderBaseEntity.class);
        List<RelOrderStEntity> relOrderSt = JSON.parseArray(JSON.toJSONString(params.get("relOrderSt")), RelOrderStEntity.class);
        return toAjax(service.update(entity, relOrderSt));
    }

    /**
     * 删除指令实例
     */
    @Log(title = "指令实例", businessType = BusinessType.DELETE)
    @DeleteMapping("/v1/DELETE/order/case/{ids}")
    public AjaxResult remove(@PathVariable long[] ids) {
        service.delete(ids);
        return success();
    }
}
